package com.nstar.hacocwebfx.view.components
{
	import flash.display.Graphics;
	
	import mx.containers.Canvas;
	import mx.controls.Label;

	public class Group extends Canvas
	{
		private var titleBar:Canvas ;
		private var labelText:Label = new Label();
		private var titleOffsetX:Number = 10;
		private var tempMask:Canvas = new Canvas();
		
		[Bindable]
		private var _title:String = "";
		
		public function Group()
		{
			//TODO: implement function
			super();
			this.styleName = "group";
		}
		
		public function set title(value:String):void{
			this._title = value;
			labelText.text = _title;
			labelText.validateNow();
			//重新设置蒙板
			generateMask(labelText.textWidth,labelText.textHeight);
		}
		
		public function get title():String{
			return this._title;
		}
		
		
		override protected function createChildren() : void{
			super.createChildren();
			//添加标题
			if(this._title!=null && this._title!=""){
				titleBar = new Canvas();
				labelText.text = this._title;
				titleBar.addChild(labelText);
				this.addChild(titleBar);
				titleBar.x = titleOffsetX;
				labelText.validateNow();
				//生成蒙板
				generateMask(labelText.textWidth,labelText.textHeight);
				this.addChild(tempMask);
				this.mask = tempMask;				
			}
		}
		
		//生成蒙板
		private function generateMask(textWidth:Number,textHeight:Number):void{
			tempMask.width = this.width ;
			tempMask.height = this.height;
			
			var points:Array = new Array();
			points=[{x:2,y:2},{x:titleOffsetX-2,y:2},{x:titleOffsetX-2,y:textHeight},
					{x:titleOffsetX+textWidth+6,y:textHeight},{x:titleOffsetX+textWidth+6,y:2},
					{x:this.width,y:2},{x:this.width,y:this.height},{x:2,y:this.height},{x:2,y:2}];
			var g:Graphics = tempMask.graphics;
			g.clear();
			g.beginFill(0xFF0000);
			g.moveTo(points[0].x,points[0].y);
			for each(var item:Object in points){
				g.lineTo(item.x,item.y);
			}
			g.endFill();
		}
		
	}
}